package de.baumann.browser.activitys.card

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import de.baumann.browser.R
import de.baumann.browser.adapter.CardListAdapter
import de.baumann.browser.adapter.RvItemClickListener
import de.baumann.browser.api.net.vo.Card
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.ICardListView
import de.baumann.browser.present.CardListPresenter
import de.baumann.browser.utils.getUserName
import de.baumann.browser.utils.put
import kotlinx.android.synthetic.main.activity_card_list.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2019/12/19 17:34
 *@discription: 黑卡选择页面
 */

class CardListActivity : BaseOdinActivity<ICardListView, CardListPresenter>(), ICardListView {
    private lateinit var cardListAdapter: CardListAdapter

    companion object {
        fun startCardListActivity(context: Context) {
            context.startActivity(Intent(context, CardListActivity::class.java))
        }
    }

    override fun getTitleText(): String {
        return "用户成长计划"
    }

    override fun getLayout(): Int {
        return R.layout.activity_card_list
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        initToolbar(commToolBar, commTitle)
        tvCardOwnerName.text = "尊敬的${getUserName()}"
        rvCardList.layoutManager = LinearLayoutManager(mContext)
        cardListAdapter = CardListAdapter(mContext, object : RvItemClickListener {
            override fun onClick(position: Int) {
                CardDetailActivity.startCardDetailActivity(this@CardListActivity, cardListAdapter.getCard(position), 1405)
            }
        })
        rvCardList.adapter = cardListAdapter
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1405 && resultCode == Activity.RESULT_OK) {
            finish()
        }
    }


    override fun initData() {
        presenter.getCardList()
    }

    override fun createPresenter(): CardListPresenter {
        return CardListPresenter()
    }

    override fun createView(): ICardListView {
        return this
    }

    override fun setCards(cards: List<Card>) {
        cardListAdapter.setCards(cards)
    }
}
